package qianfeng.com.meijiadaka.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.daimajia.slider.library.Indicators.PagerIndicator;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.DefaultSliderView;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import butterknife.BindView;
import butterknife.ButterKnife;
import qianfeng.com.meijiadaka.CommodityDtailsActivity;
import qianfeng.com.meijiadaka.HeadCategoryActivity;
import qianfeng.com.meijiadaka.R;
import qianfeng.com.meijiadaka.adapter.ShopAdapter;
import qianfeng.com.meijiadaka.adapter.ShopHeadRecyclerAdapter;
import qianfeng.com.meijiadaka.bean.Bean;


public class MallFragment extends Fragment {

    private String path = "http://mall.naildaka.com/vapi/v3/mall/home";

    //主布局listView
    @BindView(R.id.shop_listView)
    ListView mListView;
    private View mView,mHeadView;
    //轮播一
    private SliderLayout mSl;
    private PagerIndicator mPi;
    private RecyclerView mRecyclerView;
    private Bean mBean;
    //适配器
    private ShopAdapter mAdapter;
    private ShopHeadRecyclerAdapter mHeadRecyclerAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_mall,container,false);
        ButterKnife.bind(this,mView);
        //初始化轮播
        initHeader();
        return mView;
    }

    private void initHeader() {
        mHeadView = LayoutInflater.from(getActivity()).inflate(R.layout.shop_head,null);
        mSl = (SliderLayout) mHeadView.findViewById(R.id.slider);
        mPi = (PagerIndicator) mHeadView.findViewById(R.id.custom_indicator);
        mRecyclerView = (RecyclerView) mHeadView.findViewById(R.id.head_recyclerView);
        mBean = new Bean();
        for (int i = 0; i <6 ; i++) {
            loadData(i);
        }
        mSl.setDuration(2000);
        loadData1();

    }


    //加载网络数据
    private void loadData(final int pos){
        RequestParams rp = new RequestParams(path);
        rp.setConnectTimeout(3000);
        x.http().get(rp, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                mBean = new Gson().fromJson(result,Bean.class);
                DefaultSliderView defaultSliderView = new DefaultSliderView(getActivity());
                defaultSliderView.image(mBean.getResult().getTopAds().get(pos).getAdPicture());
                //轮播点击事件
                defaultSliderView.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
                    @Override
                    public void onSliderClick(BaseSliderView slider) {
                       Intent intent = new Intent(getActivity(), CommodityDtailsActivity.class);
                        intent.putExtra("commodityId", mBean.getResult().getTopAds().get(pos).getCommodityId());
                        getActivity().startActivity(intent);
                    }
                });
                mSl.addSlider(defaultSliderView);

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e( "onError: ","错误" );
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Log.e("onSuccess: ","完成" );
            }

            @Override
            public void onFinished() {
                Log.e("onFinished: ","结束" );
            }
        });
    }

    //加载网络数据
    private void loadData1(){
        RequestParams rp = new RequestParams(path);
        rp.setConnectTimeout(3000);
        x.http().get(rp, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                mBean = new Gson().fromJson(result,Bean.class);

                mHeadRecyclerAdapter = new ShopHeadRecyclerAdapter(mBean,getActivity());
                GridLayoutManager manager = new GridLayoutManager(getActivity(),4);
                manager.setOrientation(GridLayoutManager.VERTICAL);
                mRecyclerView.setLayoutManager(manager);
                mRecyclerView.setAdapter(mHeadRecyclerAdapter);
                mListView.addHeaderView(mHeadView);
                //点击事件
                mHeadRecyclerAdapter.setOnItemClickListener(new ShopHeadRecyclerAdapter.OnItemClickListener() {
                    @Override
                    public void onClick(int index) {
                        Intent intent = new Intent(getActivity(), HeadCategoryActivity.class);
                        intent.putExtra("categroyId",mBean.getResult().getToolTypes().get(index).getCategoryId());
                        intent.putExtra("title",mBean.getResult().getToolTypes().get(index).getCategoryName());
                        getActivity().startActivity(intent);
                    }
                });

                mAdapter = new ShopAdapter(getActivity(),mBean);
                mListView.setAdapter(mAdapter);

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e( "onError: ","错误" );
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Log.e("onSuccess: ","完成" );
            }

            @Override
            public void onFinished() {
                Log.e("onFinished: ","结束" );
            }
        });
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        mSl.stopAutoCycle();
    }




}
